package cn.big.bigd.biocode.configure;

import com.fasterxml.jackson.databind.DeserializationFeature;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.DispatcherType;

/**
 * Created by Dong Zou on 1/19/2016.
 */

@Configuration
public class AppConfigure {
    @Bean
    PasswordEncoder getPasswordEncoder() {

        return new BCryptPasswordEncoder();
        //  return new Md5PasswordEncoder();
    }
    @Bean
    public FilterRegistrationBean sitemeshFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MySiteMeshFilter());
        filterRegistrationBean.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.ERROR);
        return filterRegistrationBean;
    }

    @Bean
    public Jackson2ObjectMapperBuilder filteringObjectMapperBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
//        builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        builder.featuresToEnable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        return builder;
    }

    @Bean
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(final CommonInterceptor commonInterceptor){
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                super.addInterceptors(registry);
                registry.addInterceptor(commonInterceptor).addPathPatterns("/**");
            }
        };
    }
}
